package com.megvii.koala.data.verify;

import com.megvii.koala.constance.ErrorCode;
import com.megvii.koala.constance.PurposeType;

/**
 * Created by linyue on 15/12/29.
 */
public class CreatureVerifier
{
    public static final int NAME_MAX = 15;

    public static final int REMARK_MAX = 30;

    public static final int DEPARTMENT_MAX = 30;

    public static final int PHONE_MAX = 30;

    public static final int DESCRIPTION_MAX = 30;

    public static final int RANK_MAX = 15;

    public static final int TITLE_MAX = 15;

    public static final int EMAIL_MAX = 30;

    /**
     * endtime比starttime最少多多少
     */
    public static final int VISIT_TIME_MIN = 2 * 60 * 60 * 1000;

    public static int veryfyAddVisitor(String name, long startTime, long endTime, String description, String remark, int purpose)
    {
        int code = ErrorCode.NO_ERROR;
        code = verifyName(name);
        if (code != ErrorCode.NO_ERROR)
        {
            return code;
        }
        code = verifyTime(startTime, endTime);
        if (code != ErrorCode.NO_ERROR)
        {
            return code;
        }
        code = verifyDescription(description);
        if (code != ErrorCode.NO_ERROR)
        {
            return code;
        }
        code = verifyRemark(remark);
        if (code != ErrorCode.NO_ERROR)
        {
            return code;
        }
        code = verifyPurpose(purpose);
        if (code != ErrorCode.NO_ERROR)
        {
            return code;
        }
        return ErrorCode.NO_ERROR;
    }

    public static int veryfyAddVisitor(String name, String interviewee, String department,
            String phone, String description, String remark, int purpose)
    {
        int code = ErrorCode.NO_ERROR;
        code = verifyName(name);
        if (code != ErrorCode.NO_ERROR)
        {
            return code;
        }
        code = verifyName(interviewee);
        if (code != ErrorCode.NO_ERROR)
        {
            return code == ErrorCode.NAME_NULL ? ErrorCode.INTERVIEWEE_NAME_NULL : ErrorCode.INTERVIEWEE_NAME_LIMIT;
        }
        code = verifyDepartment(department);
        if (code != ErrorCode.NO_ERROR)
        {
            return code;
        }
        code = verifyPhone(phone);
        if (code != ErrorCode.NO_ERROR)
        {
            return code;
        }
        code = verifyDescription(description);
        if (code != ErrorCode.NO_ERROR)
        {
            return code;
        }
        code = verifyRemark(remark);
        if (code != ErrorCode.NO_ERROR)
        {
            return code;
        }
        code = verifyPurpose(purpose);
        if (code != ErrorCode.NO_ERROR)
        {
            return code;
        }
        return ErrorCode.NO_ERROR;
    }

    public static int verifyAddStaff(String name, String rank, String department, String title,
            String email)
    {
        int code = ErrorCode.NO_ERROR;
        code = verifyName(name);
        if (code != ErrorCode.NO_ERROR)
        {
            return code;
        }
        code = verifyRank(rank);
        if (code != ErrorCode.NO_ERROR)
        {
            return code;
        }
        code = verifyDepartment(department);
        if (code != ErrorCode.NO_ERROR)
        {
            return code;
        }
        code = verifyTitle(title);
        if (code != ErrorCode.NO_ERROR)
        {
            return code;
        }
        code = verifyEmail(email);
        if (code != ErrorCode.NO_ERROR)
        {
            return code;
        }
        return ErrorCode.NO_ERROR;
    }

    public static int verifyName(String name)
    {
        if (name == null || name.length() == 0)
        {
            return ErrorCode.NAME_NULL;
        }
        else if (name.length() > NAME_MAX)
        {
            return ErrorCode.NAME_LIMIT;
        }
        else
        {
            return ErrorCode.NO_ERROR;
        }
    }

    public static int verifyTime(long startTime, long endTime)
    {
        return endTime - startTime < VISIT_TIME_MIN ? ErrorCode.VISIT_TIME_TOO_SHORT : ErrorCode.NO_ERROR;
    }

    public static int verifyRemark(String remark)
    {
        if (remark != null && remark.length() > REMARK_MAX)
        {
            return ErrorCode.REMARK_LIMIT;
        }
        else
        {
            return ErrorCode.NO_ERROR;
        }
    }

    public static int verifyDepartment(String department)
    {
        if (department != null && department.length() > DEPARTMENT_MAX)
        {
            return ErrorCode.DEPARTMENT_LIMIT;
        }
        else
        {
            return ErrorCode.NO_ERROR;
        }
    }

    public static int verifyPhone(String phone)
    {
        if (phone != null && phone.length() > PHONE_MAX)
        {
            return ErrorCode.PHONE_LIMIT;
        }
        else
        {
            return ErrorCode.NO_ERROR;
        }
    }

    public static int verifyDescription(String description)
    {
        if (description != null && description.length() > DESCRIPTION_MAX)
        {
            return ErrorCode.DESCRIPTION_LIMIT;
        }
        else
        {
            return ErrorCode.NO_ERROR;
        }
    }

    public static int verifyRank(String rank)
    {
        if (rank != null && rank.length() > RANK_MAX)
        {
            return ErrorCode.RANK_LIMIT;
        }
        else
        {
            return ErrorCode.NO_ERROR;
        }
    }

    public static int verifyTitle(String title)
    {
        if (title != null && title.length() > TITLE_MAX)
        {
            return ErrorCode.TITLE_LIMIT;
        }
        else
        {
            return ErrorCode.NO_ERROR;
        }
    }

    public static int verifyEmail(String email)
    {
        if (email != null && email.length() > EMAIL_MAX)
        {
            return ErrorCode.EMAIL_LIMIT;
        }
        else
        {
            return ErrorCode.NO_ERROR;
        }
    }

    public static int verifyPurpose(int purpose)
    {
        if (purpose < 0 || purpose >= PurposeType.PURPOSES.length)
        {
            return ErrorCode.PURPOSE_ILLEGAL;
        }
        else
        {
            return ErrorCode.NO_ERROR;
        }
    }
}
